package com.dw.etl.core.springbatch.job;

import com.dw.etl.core.springbatch.processor.DimHandleProcessor;
import com.dw.etl.core.springbatch.processor.NullHandleProcessor;
import com.dw.etl.core.springbatch.writer.InsertWriter;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by liujunlei on 2021/5/20.
 */
@Configuration
public class DwToDmJob {

    @Resource
    private JobBuilderFactory jobBuilderFactory;    //用于构建JOB

    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    PlatformTransactionManager transactionManager;

    @Autowired
    NullHandleProcessor nullHandleProcessor;

    @Autowired
    DimHandleProcessor dimHandleProcessor;


    /**
     * 简单的查询，写入。适用于医院数据到ods的过程
     * @return
     */
    @Bean
    public Job dwToDmJobBean(){
        return jobBuilderFactory.get("dwToDmJobBean")
                .incrementer(new RunIdIncrementer()) //防止重复的
                .start(dwToDmStepBean(null,null))//为Job指定Step
                .build();
    }


    @Bean
    public Step dwToDmStepBean(InsertWriter insertWriter, ItemReader simpleReaderBean) {
        return this.stepBuilderFactory.get("odsToDwStep1Bean")
                .transactionManager(transactionManager)
                .<Map<String,Object>, Map<String,Object>>chunk(1000)
                .reader(simpleReaderBean)
                .writer(insertWriter)
                .build();
    }


}
